package com.sl.au.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sl.au.entity.ContractInfo;
import com.sl.au.entity.EXDEViExdeBeforeContractWithSign;
import com.sl.au.entity.ExdeBeforeContract;
import com.sl.au.entity.ExdeBeforeContractCounterSign;
import com.sl.au.entity.ExdeBeforeContractDetailParts;
import com.sl.au.entity.ExdeBeforeContractDetails;
import com.sl.au.entity.ExdeBeforeContractNotices;
import com.sl.au.entity.ExdeBeforeContractSpecialClause;
import com.sl.au.entity.ExdeBeforeContractWGDetail;
import com.sl.au.entity.Quote;
import com.sl.au.entity.User;
import com.sl.au.repository.ContractRepository;
import com.sl.au.repository.EXDEViExdeBeforeContractWithSignRespository;
import com.sl.au.repository.ExdeBeforeContractDetailPartsRepository;
import com.sl.au.repository.ExdeBeforeContractDetailsRepository;
import com.sl.au.repository.ExdeBeforeContractHQDetailRepository;
import com.sl.au.repository.ExdeBeforeContractNoticesRepository;
import com.sl.au.repository.ExdeBeforeContractRepository;
import com.sl.au.repository.ExdeBeforeContractSpecialClauseRepository;
import com.sl.au.repository.ExdeBeforeContractWGDetailRepository;
import com.sl.au.service.AuditNodeService;
import com.sl.au.service.ExdeBeforeContractService;

@Service("ExdeBeforeContractService")
@Transactional
public class ExdeBeforeContractServiceImpl implements ExdeBeforeContractService{
	
	@Autowired
	ExdeBeforeContractRepository exdeBeforeContractRepository;
	
	@Autowired
	ExdeBeforeContractDetailsRepository exdeBeforeContractDetailsRepository;
	
	@Autowired
	ExdeBeforeContractDetailPartsRepository exdeBeforeContractDetailPartsRepository;
	
	@Autowired
	ExdeBeforeContractNoticesRepository exdeBeforeContractNoticesRepository;
	
	@Autowired
	ExdeBeforeContractSpecialClauseRepository exdeBeforeContractSpecialClauseRepository;
	
	@Autowired
	ExdeBeforeContractWGDetailRepository exdeBeforeContractWGDetailRepository;
	
	@Autowired
	ExdeBeforeContractHQDetailRepository exdeBeforeContractHQDetailRepository;
	@Autowired
	EXDEViExdeBeforeContractWithSignRespository eXDEViExdeBeforeContractWithSignRespository;
	
	@Autowired
	AuditNodeService auditNodeService;
	
	@PersistenceContext
	EntityManager em;
	
	@Override
	public List<User> getAllSuperiorMenBySuperior(String superior) {
		return this.exdeBeforeContractDetailsRepository.getAllSuperiorMenBySuperior(superior);
	}

	
	@Override
	public ExdeBeforeContractDetailsRepository getExdeBeforeContractDetailsRepository() {
		return this.exdeBeforeContractDetailsRepository;
	}
	
	@Override
	public ExdeBeforeContract getBeforeContract(String id) {
		return exdeBeforeContractRepository.getBeforeContract(id);
	}

	@Override
	public String deleteExdeBeforeContractDetails(String ids) {
		String[] idArray = ids.split(",");
		String maiId = "";
	    for (int i=0;i<idArray.length;i++) {
	    	maiId = exdeBeforeContractDetailsRepository.findOne(idArray[i]).getbContractId();
	    	exdeBeforeContractDetailPartsRepository.deleteByMainId(idArray[i]);
			exdeBeforeContractDetailsRepository.deleteById(idArray[i]);
	    }
	    return maiId;
	}
	
	

	@Override
	public void deleteExdeBeforeContractNotices(String ids) {
		String[] idArray = ids.split(",");
		ExdeBeforeContractNotices a = exdeBeforeContractNoticesRepository.findOne(idArray[0]);
	    for (int i=0;i<idArray.length;i++) {
	    	exdeBeforeContractNoticesRepository.deleteById(idArray[i]);
	    }
	    List<ExdeBeforeContractNotices> pages = null;
	    Pageable pageable = new PageRequest(0 / Integer.MAX_VALUE, Integer.MAX_VALUE, Sort.Direction.ASC, "nOrderNumber");
		pages = exdeBeforeContractRepository.getExdeBeforeContractNoticesByBeforeId(a.getbContractId(),pageable).getContent();
		int i = 0;
		for(ExdeBeforeContractNotices page : pages){
			page.setnOrderNumber(++i);
			exdeBeforeContractNoticesRepository.save(page);
		}
	}

	@Override
	public void deleteExdeBeforeContractSpecialClause(String ids) {
		String[] idArray = ids.split(",");
		ExdeBeforeContractSpecialClause a = exdeBeforeContractSpecialClauseRepository.findOne(idArray[0]);
	    for (int i=0;i<idArray.length;i++) {
	    	exdeBeforeContractSpecialClauseRepository.deleteById(idArray[i]);
	    }
	    List<ExdeBeforeContractSpecialClause> pages = null;
	    Pageable pageable = new PageRequest(0 / Integer.MAX_VALUE, Integer.MAX_VALUE, Sort.Direction.ASC, "nOrderNumber");
		pages = exdeBeforeContractRepository.getExdeBeforeContractSpecialClauseByBeforeId(a.getbContractId(),pageable).getContent();
		int i = 0;
		for(ExdeBeforeContractSpecialClause page : pages){
			page.setnOrderNumber(++i);
			exdeBeforeContractSpecialClauseRepository.save(page);
		}
			
	}

	@Override
	public void deleteExdeBeforeContractWGDetail(String ids) {
		String[] idArray = ids.split(",");
		ExdeBeforeContractWGDetail a = exdeBeforeContractWGDetailRepository.findOne(idArray[0]);
	    for (int i=0;i<idArray.length;i++) {
	    	exdeBeforeContractWGDetailRepository.deleteById(idArray[i]);
	    }
	    Pageable pageable = new PageRequest(0 / Integer.MAX_VALUE, Integer.MAX_VALUE, Sort.Direction.ASC, "nOrderNumber");
	    List<ExdeBeforeContractWGDetail> pages = exdeBeforeContractRepository.getExdeBeforeContractWGDetailByBeforeId(a.getbContractId(),pageable).getContent();
		int i = 0;
		for(ExdeBeforeContractWGDetail page : pages){
			page.setnOrderNumber(++i);
			exdeBeforeContractWGDetailRepository.save(page);
		}
	}

	@Override
	public String deleteExdeBeforeContractDetailParts(String ids) {
		String[] idArray = ids.split(",");
		String idReturn = "";
		ExdeBeforeContractDetailParts a = exdeBeforeContractDetailPartsRepository.findOne(idArray[0]);
	    for (int i=0;i<idArray.length;i++) {
	    	idReturn = exdeBeforeContractDetailPartsRepository.getOne(idArray[i]).getbContractDetailId();
	    	exdeBeforeContractDetailPartsRepository.deleteById(idArray[i]);
	    }
	    
	    Pageable pageable = new PageRequest(0 / Integer.MAX_VALUE, Integer.MAX_VALUE, Sort.Direction.ASC, "nOrderNumber");
	    List<ExdeBeforeContractDetailParts> pages = exdeBeforeContractRepository.getExdeBeforeContractDetailPartsByDetailId(a.getbContractDetailId(),pageable).getContent();
		int i = 0;
		for(ExdeBeforeContractDetailParts page : pages){
			page.setnOrderNumber(++i);
			exdeBeforeContractDetailPartsRepository.save(page);
		}
	    
	    return idReturn;
	}
	
	
	
	@Override
	public void saveProcessStart(String beforeId, ProcessInstance pInst) {
		ExdeBeforeContract exdeBeforeContract = exdeBeforeContractRepository.getOne(beforeId);
		if (null == pInst || null == exdeBeforeContract)
			return;
		// 如果是重新提交审批，删除原来的审批节点
		auditNodeService.getRepository().deleteByPInstId(exdeBeforeContract.getProcessInstId());
		exdeBeforeContract.setProcessInstId(pInst.getId());
		exdeBeforeContract.setAuditState("已提交");
		exdeBeforeContract.setStartDate(new Date());
		this.getExdeBeforeContractRepository().save(exdeBeforeContract);
	}

	@Override
	public void initBaseDetailsInfo(String id,String superior) {
		//初始化合同实体
		//ExdeBeforeContract exdeBeforeContract = new ExdeBeforeContract();
		//exdeBeforeContract.setId(id);
		List<ExdeBeforeContractSpecialClause> list = new ArrayList<ExdeBeforeContractSpecialClause>();
		//初始化第一条
		ExdeBeforeContractSpecialClause exdeBeforeContractSpecialClause1 = new ExdeBeforeContractSpecialClause();
		exdeBeforeContractSpecialClause1.setcContent("预付款为定金");
		exdeBeforeContractSpecialClause1.setcMemo("");
		exdeBeforeContractSpecialClause1.setnOrderNumber(1);
		exdeBeforeContractSpecialClause1.setbContractId(id);
		list.add(exdeBeforeContractSpecialClause1);
		//初始化第二条
		ExdeBeforeContractSpecialClause exdeBeforeContractSpecialClause2 = new ExdeBeforeContractSpecialClause();
		exdeBeforeContractSpecialClause2.setcContent("除A类企业外发货款50%及以下");
		exdeBeforeContractSpecialClause2.setcMemo("");
		exdeBeforeContractSpecialClause2.setnOrderNumber(2);
		exdeBeforeContractSpecialClause2.setbContractId(id);
		list.add(exdeBeforeContractSpecialClause2);
		//初始化第三条
		ExdeBeforeContractSpecialClause exdeBeforeContractSpecialClause3 = new ExdeBeforeContractSpecialClause();
		exdeBeforeContractSpecialClause3.setcContent("除A类企业外付款节点未封口");
		exdeBeforeContractSpecialClause3.setcMemo("");
		exdeBeforeContractSpecialClause3.setnOrderNumber(3);
		exdeBeforeContractSpecialClause3.setbContractId(id);
		list.add(exdeBeforeContractSpecialClause3);
		//初始化第四条
		ExdeBeforeContractSpecialClause exdeBeforeContractSpecialClause4 = new ExdeBeforeContractSpecialClause();
		exdeBeforeContractSpecialClause4.setcContent("延期交货、资料提供、售后服务、合同赔偿等罚款(赔偿)总额超过合同总额5%或未封口");
		exdeBeforeContractSpecialClause4.setcMemo("");
		exdeBeforeContractSpecialClause4.setnOrderNumber(4);
		exdeBeforeContractSpecialClause4.setbContractId(id);
		list.add(exdeBeforeContractSpecialClause4);
		//初始化第五条
		ExdeBeforeContractSpecialClause exdeBeforeContractSpecialClause5 = new ExdeBeforeContractSpecialClause();
		exdeBeforeContractSpecialClause5.setcContent("赔偿无限、所有或一切责任等");
		exdeBeforeContractSpecialClause5.setcMemo("");
		exdeBeforeContractSpecialClause5.setnOrderNumber(5);
		exdeBeforeContractSpecialClause5.setbContractId(id);
		list.add(exdeBeforeContractSpecialClause5);
		//初始化第六条
		ExdeBeforeContractSpecialClause exdeBeforeContractSpecialClause6 = new ExdeBeforeContractSpecialClause();
		if(superior == "成套处"){
			exdeBeforeContractSpecialClause6.setcContent("质保期超过安装调试合格正式运行12个月或货到现场之日起18个月,以先到为准");
		}else{
			exdeBeforeContractSpecialClause6.setcContent("质保期超过安装调试合格正式运行12个月或货到现场之日起18个月");
		}
		exdeBeforeContractSpecialClause6.setcMemo("");
		exdeBeforeContractSpecialClause6.setnOrderNumber(6);
		exdeBeforeContractSpecialClause6.setbContractId(id);
		list.add(exdeBeforeContractSpecialClause6);
		for (int i = 0; i < list.size(); i++) {
            em.persist(list.get(i));
        }
		em.flush();
        em.clear();
        //初始化第一条 wgj
        List<ExdeBeforeContractWGDetail> list1 = new ArrayList<ExdeBeforeContractWGDetail>();
        ExdeBeforeContractWGDetail exdeBeforeContractWGDetail = new ExdeBeforeContractWGDetail();
        exdeBeforeContractWGDetail.setcContent("");
        exdeBeforeContractWGDetail.setcReason("");
        exdeBeforeContractWGDetail.setcWGName("安全阀");
        exdeBeforeContractWGDetail.setbContractId(id);
        exdeBeforeContractWGDetail.setnCount(0);
        exdeBeforeContractWGDetail.setnOrderNumber(1);
        list1.add(exdeBeforeContractWGDetail);
        
        //初始化第二条 wgj
        ExdeBeforeContractWGDetail exdeBeforeContractWGDetail1 = new ExdeBeforeContractWGDetail();
        exdeBeforeContractWGDetail1.setcContent("");
        exdeBeforeContractWGDetail1.setcReason("");
        exdeBeforeContractWGDetail1.setcWGName("电机");
        exdeBeforeContractWGDetail1.setbContractId(id);
        exdeBeforeContractWGDetail1.setnCount(0);
        exdeBeforeContractWGDetail1.setnOrderNumber(2);
        list1.add(exdeBeforeContractWGDetail1);
        
        //初始化第三条 wgj
        ExdeBeforeContractWGDetail exdeBeforeContractWGDetail2 = new ExdeBeforeContractWGDetail();
        exdeBeforeContractWGDetail2.setcContent("");
        exdeBeforeContractWGDetail2.setcReason("");
        exdeBeforeContractWGDetail2.setcWGName("轴承");
        exdeBeforeContractWGDetail2.setbContractId(id);
        exdeBeforeContractWGDetail2.setnCount(0);
        exdeBeforeContractWGDetail2.setnOrderNumber(3);
        list1.add(exdeBeforeContractWGDetail2);
        
        //初始化第四条 wgj
        ExdeBeforeContractWGDetail exdeBeforeContractWGDetail3 = new ExdeBeforeContractWGDetail();
        exdeBeforeContractWGDetail3.setcContent("");
        exdeBeforeContractWGDetail3.setcReason("");
        exdeBeforeContractWGDetail3.setcWGName("仪表");
        exdeBeforeContractWGDetail3.setbContractId(id);
        exdeBeforeContractWGDetail3.setnCount(0);
        exdeBeforeContractWGDetail3.setnOrderNumber(4);
        list1.add(exdeBeforeContractWGDetail3);
        
        //初始化第五条 wgj
        ExdeBeforeContractWGDetail exdeBeforeContractWGDetail4 = new ExdeBeforeContractWGDetail();
        exdeBeforeContractWGDetail4.setcContent("");
        exdeBeforeContractWGDetail4.setcReason("");
        exdeBeforeContractWGDetail4.setcWGName("联轴器");
        exdeBeforeContractWGDetail4.setbContractId(id);
        exdeBeforeContractWGDetail4.setnCount(0);
        exdeBeforeContractWGDetail4.setnOrderNumber(5);
        list1.add(exdeBeforeContractWGDetail4);
        
        //初始化第六条 wgj
        ExdeBeforeContractWGDetail exdeBeforeContractWGDetail5 = new ExdeBeforeContractWGDetail();
        exdeBeforeContractWGDetail5.setcContent("");
        exdeBeforeContractWGDetail5.setcReason("");
        exdeBeforeContractWGDetail5.setcWGName("油站");
        exdeBeforeContractWGDetail5.setbContractId(id);
        exdeBeforeContractWGDetail5.setnCount(0);
        exdeBeforeContractWGDetail5.setnOrderNumber(6);
        list1.add(exdeBeforeContractWGDetail5);
        
        //初始化第七条 wgj
        ExdeBeforeContractWGDetail exdeBeforeContractWGDetail6 = new ExdeBeforeContractWGDetail();
        exdeBeforeContractWGDetail6.setcContent("");
        exdeBeforeContractWGDetail6.setcReason("");
        exdeBeforeContractWGDetail6.setcWGName("增/减速机");
        exdeBeforeContractWGDetail6.setbContractId(id);
        exdeBeforeContractWGDetail6.setnCount(0);
        exdeBeforeContractWGDetail6.setnOrderNumber(7);
        list1.add(exdeBeforeContractWGDetail6);
        
        //初始化第八条 wgj
        ExdeBeforeContractWGDetail exdeBeforeContractWGDetail7 = new ExdeBeforeContractWGDetail();
        exdeBeforeContractWGDetail7.setcContent("");
        exdeBeforeContractWGDetail7.setcReason("");
        exdeBeforeContractWGDetail7.setcWGName("最小流量阀");
        exdeBeforeContractWGDetail7.setbContractId(id);
        exdeBeforeContractWGDetail7.setnCount(0);
        exdeBeforeContractWGDetail7.setnOrderNumber(8);
        list1.add(exdeBeforeContractWGDetail7);
        
        //初始化第九条 wgj
        ExdeBeforeContractWGDetail exdeBeforeContractWGDetail8 = new ExdeBeforeContractWGDetail();
        exdeBeforeContractWGDetail8.setcContent("");
        exdeBeforeContractWGDetail8.setcReason("");
        exdeBeforeContractWGDetail8.setcWGName("其他");
        exdeBeforeContractWGDetail8.setbContractId(id);
        exdeBeforeContractWGDetail8.setnCount(0);
        exdeBeforeContractWGDetail8.setnOrderNumber(9);
        list1.add(exdeBeforeContractWGDetail8);
        
        for (int j = 0; j < list1.size(); j++) {
            em.persist(list1.get(j));
        }
		em.flush();
        em.clear();
	}

	@Override
	public void initDetailsDetailsInfo(ExdeBeforeContractDetails exdeBeforeContractDetails) {
		String detailId= exdeBeforeContractDetails.getId();
		List<ExdeBeforeContractDetailParts> list1 = new ArrayList<ExdeBeforeContractDetailParts>();
        ExdeBeforeContractDetailParts exdeBeforeContractDetailParts1 = new ExdeBeforeContractDetailParts();
        exdeBeforeContractDetailParts1.setbContractDetailId(detailId);
        exdeBeforeContractDetailParts1.setcName(exdeBeforeContractDetails.getcProductName());
        exdeBeforeContractDetailParts1.setnOrderNumber(1);
        exdeBeforeContractDetailParts1.setnContractPrice(exdeBeforeContractDetails.getnContractPrice());
        exdeBeforeContractDetailParts1.setnAddOrReducePrice(exdeBeforeContractDetails.getnAddOrReducePrice());
        exdeBeforeContractDetailParts1.setnOutPrice(exdeBeforeContractDetails.getnOutPrice());
        exdeBeforeContractDetailParts1.setnCount(exdeBeforeContractDetails.getnCount());
        list1.add(exdeBeforeContractDetailParts1);
		
        ExdeBeforeContractDetailParts exdeBeforeContractDetailParts2 = new ExdeBeforeContractDetailParts();
        exdeBeforeContractDetailParts2.setbContractDetailId(detailId);
        exdeBeforeContractDetailParts2.setcName("电机(汽轮机)");
        exdeBeforeContractDetailParts2.setnOrderNumber(2);
        list1.add(exdeBeforeContractDetailParts2);
         
        ExdeBeforeContractDetailParts exdeBeforeContractDetailParts3 = new ExdeBeforeContractDetailParts();
        exdeBeforeContractDetailParts3.setbContractDetailId(detailId);
        exdeBeforeContractDetailParts3.setcName("透平");
        exdeBeforeContractDetailParts3.setnOrderNumber(3);
        list1.add(exdeBeforeContractDetailParts3);
        
        ExdeBeforeContractDetailParts exdeBeforeContractDetailParts4 = new ExdeBeforeContractDetailParts();
        exdeBeforeContractDetailParts4.setbContractDetailId(detailId);
        exdeBeforeContractDetailParts4.setcName("减速机/增速机");
        exdeBeforeContractDetailParts4.setnOrderNumber(4);
        list1.add(exdeBeforeContractDetailParts4);
        
        ExdeBeforeContractDetailParts exdeBeforeContractDetailParts5 = new ExdeBeforeContractDetailParts();
        exdeBeforeContractDetailParts5.setbContractDetailId(detailId);
        exdeBeforeContractDetailParts5.setcName("润滑(冲洗)油站");
        exdeBeforeContractDetailParts5.setnOrderNumber(5);
        list1.add(exdeBeforeContractDetailParts5);
        
        ExdeBeforeContractDetailParts exdeBeforeContractDetailParts6 = new ExdeBeforeContractDetailParts();
        exdeBeforeContractDetailParts6.setbContractDetailId(detailId);
        exdeBeforeContractDetailParts6.setcName("PLC(PLC柜)");
        exdeBeforeContractDetailParts6.setnOrderNumber(6);
        list1.add(exdeBeforeContractDetailParts6);
        
        ExdeBeforeContractDetailParts exdeBeforeContractDetailParts7 = new ExdeBeforeContractDetailParts();
        exdeBeforeContractDetailParts7.setbContractDetailId(detailId);
        exdeBeforeContractDetailParts7.setcName("现场操作柱(现场接线箱)");
        exdeBeforeContractDetailParts7.setnOrderNumber(7);
        list1.add(exdeBeforeContractDetailParts7);
        
        ExdeBeforeContractDetailParts exdeBeforeContractDetailParts8 = new ExdeBeforeContractDetailParts();
        exdeBeforeContractDetailParts8.setbContractDetailId(detailId);
        exdeBeforeContractDetailParts8.setcName("安全阀");
        exdeBeforeContractDetailParts8.setnOrderNumber(8);
        list1.add(exdeBeforeContractDetailParts8);
        
        ExdeBeforeContractDetailParts exdeBeforeContractDetailParts9 = new ExdeBeforeContractDetailParts();
        exdeBeforeContractDetailParts9.setbContractDetailId(detailId);
        exdeBeforeContractDetailParts9.setcName("进口缓冲器");
        exdeBeforeContractDetailParts9.setnOrderNumber(9);
        list1.add(exdeBeforeContractDetailParts9);
        
        ExdeBeforeContractDetailParts exdeBeforeContractDetailParts10 = new ExdeBeforeContractDetailParts();
        exdeBeforeContractDetailParts10.setbContractDetailId(detailId);
        exdeBeforeContractDetailParts10.setcName("出口缓冲器");
        exdeBeforeContractDetailParts10.setnOrderNumber(10);
        list1.add(exdeBeforeContractDetailParts10);
        
        ExdeBeforeContractDetailParts exdeBeforeContractDetailParts11 = new ExdeBeforeContractDetailParts();
        exdeBeforeContractDetailParts11.setbContractDetailId(detailId);
        exdeBeforeContractDetailParts11.setcName("最小流量阀");
        exdeBeforeContractDetailParts11.setnOrderNumber(11);
        list1.add(exdeBeforeContractDetailParts11);
        
        ExdeBeforeContractDetailParts exdeBeforeContractDetailParts12 = new ExdeBeforeContractDetailParts();
        exdeBeforeContractDetailParts12.setbContractDetailId(detailId);
        exdeBeforeContractDetailParts12.setcName("止回阀");
        exdeBeforeContractDetailParts12.setnOrderNumber(12);
        list1.add(exdeBeforeContractDetailParts12);
        
        ExdeBeforeContractDetailParts exdeBeforeContractDetailParts13 = new ExdeBeforeContractDetailParts();
        exdeBeforeContractDetailParts13.setbContractDetailId(detailId);
        exdeBeforeContractDetailParts13.setcName("配对法兰、地脚螺栓(含紧固件和垫片)");
        exdeBeforeContractDetailParts13.setnOrderNumber(13);
        list1.add(exdeBeforeContractDetailParts13);
        
        for (int j = 0; j < list1.size(); j++) {
            em.persist(list1.get(j));
        }
		em.flush();
        em.clear();
	}

	@Override
	public List<ExdeBeforeContractDetails> getExdeBeforeContractDetailsByBeforeId(int start, int length, String id) {
		Page<ExdeBeforeContractDetails> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.ASC, "createTime");
		pages = exdeBeforeContractRepository.getExdeBeforeContractDetailsByBeforeId(id,pageable);
		return pages.getContent();
	}

	@Override
	public long getExdeBeforeContractDetailsCountByBeforeId(String id) {
		return exdeBeforeContractRepository.getExdeBeforeContractDetailsCountByBeforeId(id);
	}

	@Override
	public List<ExdeBeforeContractDetailParts> getExdeBeforeContractDetailPartsByDetailId(int start, int length,
			String beforeDetailId) {
		Page<ExdeBeforeContractDetailParts> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.ASC, "nOrderNumber");
		pages = exdeBeforeContractRepository.getExdeBeforeContractDetailPartsByDetailId(beforeDetailId,pageable);
		return pages.getContent();
	}

	@Override
	public long getExdeBeforeContractDetailPartsCountByDetailId(String beforeDetailId) {
		return exdeBeforeContractRepository.getExdeBeforeContractDetailPartsCountByDetailId(beforeDetailId);
	}

	@Override
	public List<ExdeBeforeContractNotices> getExdeBeforeContractNoticesByBeforeId(int start, int length,
			String beforeId) {
		Page<ExdeBeforeContractNotices> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.ASC, "nOrderNumber");
		pages = exdeBeforeContractRepository.getExdeBeforeContractNoticesByBeforeId(beforeId,pageable);
		return pages.getContent();
	}

	@Override
	public long getExdeBeforeContractNoticesCountByBeforeId(String beforeId) {
		return exdeBeforeContractRepository.getExdeBeforeContractNoticesCountByBeforeId(beforeId);
	}

	@Override
	public List<ExdeBeforeContractSpecialClause> getExdeBeforeContractSpecialClauseByBeforeId(int start, int length,
			String beforeId) {
		Page<ExdeBeforeContractSpecialClause> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.ASC, "nOrderNumber");
		pages = exdeBeforeContractRepository.getExdeBeforeContractSpecialClauseByBeforeId(beforeId,pageable);
		return pages.getContent();
	}

	@Override
	public long getExdeBeforeContractSpecialClauseCountByBeforeId(String beforeId) {
		return exdeBeforeContractRepository.getExdeBeforeContractSpecialClauseCountByBeforeId(beforeId);
	}

	@Override
	public List<ExdeBeforeContractWGDetail> getExdeBeforeContractWGDetailByBeforeId(int start, int length,
			String beforeId) {
		Page<ExdeBeforeContractWGDetail> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.ASC, "nOrderNumber");
		pages = exdeBeforeContractRepository.getExdeBeforeContractWGDetailByBeforeId(beforeId,pageable);
		return pages.getContent();
	}

	@Override
	public long getExdeBeforeContractWGDetailCountByBeforeId(String beforeId) {
		return exdeBeforeContractRepository.getExdeBeforeContractWGDetailCountByBeforeId(beforeId);
	}

	@Override
	public List<ExdeBeforeContract> getBeforeContracts(int start, int length, Map<String, String> condition) {
		Page<ExdeBeforeContract> pages = null;
		String dataFilter = condition.get("dataFilter");
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if(dataFilter.equals("所有")){
			pages = exdeBeforeContractRepository.getBeforeContractsByAll(condition.get("key"), pageable);
		}else if(dataFilter.equals("本部门")){
			pages = exdeBeforeContractRepository.getBeforeContractsByDept(condition.get("key"), condition.get("superior"), pageable);
		}else if(dataFilter.equals("本片区")){
			pages = exdeBeforeContractRepository.getBeforeContractsByArea(condition.get("key"), condition.get("areaName"), pageable);
		}else if(dataFilter.equals("本人")){
			pages = exdeBeforeContractRepository.getBeforeContractsByPerson(condition.get("key"), condition.get("userId"), pageable);
		}
		return pages.getContent();
	}

	@Override
	public void deleteContractById(String id) {
		Page<ExdeBeforeContractDetails> pages = null;
		pages = exdeBeforeContractRepository.getExdeBeforeContractDetailsByBeforeId(id, new PageRequest(1 / 1000, 1000, Sort.Direction.DESC, "createTime"));
		List<ExdeBeforeContractDetails> listDetails = pages.getContent();
		if(listDetails!=null && !listDetails.isEmpty()){
			for(int i =0 ;i<listDetails.size();i++){
				exdeBeforeContractRepository.deleteExdeBeforeContractDetailPartsByBeforeContractDetailsId(listDetails.get(i).getId());
			}
			//detailIds = detailIds.substring(0, detailIds.length()-1);
		}
		exdeBeforeContractRepository.deleteExdeBeforeContractDetailsByBeforeContractId(id);
		exdeBeforeContractRepository.deleteExdeBeforeContractNoticesByBeforeContractId(id);
		exdeBeforeContractRepository.deleteExdeBeforeContractSpecialClauseByBeforeContractId(id);
		exdeBeforeContractRepository.deleteExdeBeforeContractWGDetailByBeforeContractId(id);
		exdeBeforeContractRepository.deleteExdeBeforeContractById(id);
	}

	@Override
	public long getBeforeContractCount(Map<String, String> condition) {
		String dataFilter = condition.get("dataFilter");
		long count = 0;
		if(dataFilter.equals("所有")){
			count = exdeBeforeContractRepository.getBeforeContractsCountByAll(condition.get("key"));
		}else if(dataFilter.equals("本部门")){
			count = exdeBeforeContractRepository.getBeforeContractsCountByDept(condition.get("key"), condition.get("superior"));
		}else if(dataFilter.equals("本片区")){
			count = exdeBeforeContractRepository.getBeforeContractsCountByArea(condition.get("key"), condition.get("areaName"));
		}else if(dataFilter.equals("本人")){
			count = exdeBeforeContractRepository.getBeforeContractsCountByPerson(condition.get("key"), condition.get("userId"));
		}
		return count;
	}

	@Override
	public ExdeBeforeContractRepository getExdeBeforeContractRepository() {
		return this.exdeBeforeContractRepository;
	}
	
	@Override
	public ExdeBeforeContractDetailPartsRepository getExdeBeforeContractDetailPartsRespository() {
		return this.exdeBeforeContractDetailPartsRepository;
	}

	@Override
	public ExdeBeforeContractNoticesRepository getExdeBeforeContractNoticesRepository() {
		return this.exdeBeforeContractNoticesRepository;
	}

	@Override
	public ExdeBeforeContractSpecialClauseRepository getExdeBeforeContractSpecialClauseRepository() {
		return this.exdeBeforeContractSpecialClauseRepository;
	}

	@Override
	public ExdeBeforeContractWGDetailRepository getExdeBeforeContractWGDetailRepository() {
		return this.exdeBeforeContractWGDetailRepository;
	}

	@Override
	public ExdeBeforeContractHQDetailRepository getExdeBeforeContractHQDetailRepository() {
		return this.exdeBeforeContractHQDetailRepository;
	}
	
	public EXDEViExdeBeforeContractWithSignRespository getEXDEViExdeBeforeContractWithSignRespository(){
		return this.eXDEViExdeBeforeContractWithSignRespository;
	}

	@Override
	public List<ExdeBeforeContractCounterSign> getBeforeContractsHQ(int start, int length,
			String beforeId) {
		Page<ExdeBeforeContractCounterSign> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.ASC, "createTime");
		pages = exdeBeforeContractHQDetailRepository.getBeforeContractsCounterSignList(beforeId, pageable);
		return pages.getContent();
	}

	@Override
	public long getBeforeContractHQCount(String beforeId) {
		return exdeBeforeContractHQDetailRepository.getBeforeContractsCounterSignCount(beforeId);
	}

	@Override
	public List<ExdeBeforeContractCounterSign> getExdeBeforeContractCounterSignListByUserId(int start, int length,
			String userId) {
		Page<ExdeBeforeContractCounterSign> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.ASC, "createTime");
		pages = exdeBeforeContractHQDetailRepository.getBeforeContractsCounterSignByUserIdList(userId, pageable);
		return pages.getContent();
	}

	@Override
	public String submitExdeBeforeContractCounterSign(String taskId,String userId, String auditMemo) {
		List<ExdeBeforeContractCounterSign> exdeBeforeContractCounterSign = this.exdeBeforeContractHQDetailRepository.getExdeBeforeContractCounterSignByUserIdList(taskId,userId);
		if(exdeBeforeContractCounterSign.size()>0){
			for(int i=0;i<exdeBeforeContractCounterSign.size();i++){
				exdeBeforeContractCounterSign.get(i).setcAuditMemo(auditMemo);
				exdeBeforeContractCounterSign.get(i).setcAuditState(1);
				exdeBeforeContractCounterSign.get(i).setdAuditSubmitDate(new Date());
				this.exdeBeforeContractHQDetailRepository.save(exdeBeforeContractCounterSign.get(i));
			}
		}
		return exdeBeforeContractCounterSign.get(0).getExdeBeforeContract().getId();
	}

	@Override
	public void deleteExdeBeforeContractCounterSigns(String ids) {
		String[] idArray = ids.split(",");
	    for (int i=0;i<idArray.length;i++) {
	    	exdeBeforeContractHQDetailRepository.deleteById(idArray[i]);
	    }
	}

	@Override
	public List<User> getAllUserList(int start, int size, String key) {
		Page<User> pages = null;
		Pageable pageable = new PageRequest(start / size, size, Sort.Direction.ASC, "createTime");
		pages = exdeBeforeContractRepository.getAllUserList(key, pageable);
		return pages.getContent();
	}
	
	@Override
	public List<User> getUserList(int start, int size, String key) {
		Page<User> pages = null;
		Pageable pageable = new PageRequest(start / size, size, Sort.Direction.ASC, "createTime");
		pages = exdeBeforeContractRepository.getUserList(key, pageable);
		return pages.getContent();
	}
	@Override
	public long getUserCount(String key) {
		return exdeBeforeContractRepository.getUserCount(key);
	}

	@Override
	public long getAllUserCount(String key) {
		return exdeBeforeContractRepository.getAllUserCount(key);
	}

	@Override
	public void updateExdeBeforeContractCounterSignState(String beforeId) {
		ExdeBeforeContract exdeBeforeContract = exdeBeforeContractRepository.getOne(beforeId);
		long counterSignCount = exdeBeforeContractHQDetailRepository.getBeforeContractsCounterSignCount(beforeId);
		if(counterSignCount == 0){
			exdeBeforeContract.setcCounterSignState("无须会签");
			exdeBeforeContractRepository.save(exdeBeforeContract);
		}
	}

	@Override
	public void updateExdeBeforeContractCounterSignFinishState(String beforeId) {
		ExdeBeforeContract exdeBeforeContract = exdeBeforeContractRepository.getOne(beforeId);
		long counterSignCount = exdeBeforeContractHQDetailRepository.getBeforeContractsCounterSignFinishCount(beforeId);
		if(counterSignCount == 0){
			exdeBeforeContract.setcCounterSignState("已完成");
			exdeBeforeContractRepository.save(exdeBeforeContract);
		}
	}

	@Override
	public String sumExdeBeforeContractDetailsPrices(String detailId) {
		Pageable pageable = new PageRequest(0 / 1000, 1000, Sort.Direction.ASC, "createTime");
		List<ExdeBeforeContractDetailParts> list = exdeBeforeContractRepository.getExdeBeforeContractDetailPartsByDetailId(detailId, pageable).getContent();
		if(list.size() > 0){
			float nContractPriceTotal=0; 
			float nOutPriceTotal=0; 
			float nAddOrReducePriceTotal=0; 
			int nCount=0;
			for(int i=0;i<list.size();i++){
				nAddOrReducePriceTotal += list.get(i).getnAddOrReducePrice();
				nOutPriceTotal += list.get(i).getnOutPrice();
				nContractPriceTotal += list.get(i).getnContractPrice();
				//nCount +=list.get(i).getnCount();
			}
			String upId = list.get(0).getbContractDetailId();
			ExdeBeforeContractDetails exdeBeforeContractDetails = exdeBeforeContractDetailsRepository.getOne(upId);
			exdeBeforeContractDetails.setnAddOrReducePrice(nAddOrReducePriceTotal);
			exdeBeforeContractDetails.setnContractPrice(nContractPriceTotal);
			exdeBeforeContractDetails.setnOutPrice(nOutPriceTotal);
			//exdeBeforeContractDetails.setnCount(nCount);
			exdeBeforeContractDetailsRepository.save(exdeBeforeContractDetails);
			return exdeBeforeContractDetails.getbContractId();
		}else{
			return "";
		}
	}

	@Override
	public List<EXDEViExdeBeforeContractWithSign> getEXDEViExdeBeforeContractWithSignByUserId(String UserId) {
		Page<EXDEViExdeBeforeContractWithSign> pages = null;
		Pageable pageable = new PageRequest(0 / 1000, 1000, Sort.Direction.ASC, "createTime");
		pages = eXDEViExdeBeforeContractWithSignRespository.getBeforeContractsCounterSignList(UserId, pageable);
		return pages.getContent();
	}

	@Override
	public List<Float> sumExdeBeforeContractDetailsPricesZJ(String bContractId) {
		Pageable pageable = new PageRequest(0 / 1000, 1000, Sort.Direction.ASC, "cProductName");
		List<ExdeBeforeContractDetails> list = exdeBeforeContractRepository.getExdeBeforeContractDetailsByBeforeId(bContractId, pageable).getContent();
		float nTotalContractPrice=0; //合同总价
		float nTotalDisCount=0; //加减价
		for(int i=0;i<list.size();i++){
			nTotalContractPrice += list.get(i).getnContractPrice();
			nTotalDisCount += list.get(i).getnAddOrReducePrice();
		}
		ExdeBeforeContract exdeBeforeContract = exdeBeforeContractRepository.getBeforeContract(bContractId);
		nTotalContractPrice = nTotalContractPrice*10000 + exdeBeforeContract.getnDeliveryPrice()+exdeBeforeContract.getnServicePrice();
		float nDisCountPercent = nTotalContractPrice == 0 ? 0 :( nTotalDisCount*10000/nTotalContractPrice*100);//加减价比例
		exdeBeforeContract.setnTotalContractPrice(nTotalContractPrice);
		exdeBeforeContract.setnDisCountPercent(nDisCountPercent);
		exdeBeforeContractRepository.save(exdeBeforeContract);
		List<Float> a = new ArrayList<Float>();
		a.add(nTotalContractPrice);//总计
		a.add(nDisCountPercent);//加减价比例
		System.out.println(a);
		return a;
	}


	/**
	 * 保存合同签订前审批合同特殊条款子表
	 */
	@Override
	public void saveExdeBeforeContractSpecialClause(ExdeBeforeContractSpecialClause exdeBeforeContractSpecialClause,
			List<ExdeBeforeContractSpecialClause> list, boolean isInit) {
		for(int i=0;i<list.size();i++){
			ExdeBeforeContractSpecialClause a = list.get(i);
			int b = a.getnOrderNumber();
			if(isInit){
				if(b>=exdeBeforeContractSpecialClause.getnOrderNumber())
					a.setnOrderNumber(b+1);
			}else{
				ExdeBeforeContractSpecialClause old = exdeBeforeContractSpecialClauseRepository.findOne(exdeBeforeContractSpecialClause.getId());
				if(exdeBeforeContractSpecialClause.getnOrderNumber()<old.getnOrderNumber()){
					if(i<old.getnOrderNumber()-1 && i>=exdeBeforeContractSpecialClause.getnOrderNumber()-1){
						a.setnOrderNumber(b+1);
					}
				}else if(exdeBeforeContractSpecialClause.getnOrderNumber()>old.getnOrderNumber()){
					if(i>old.getnOrderNumber()-1 && i<=exdeBeforeContractSpecialClause.getnOrderNumber()-1){
						a.setnOrderNumber(b-1);
					}
				}
			}
			exdeBeforeContractSpecialClauseRepository.save(a);
		}
		exdeBeforeContractSpecialClauseRepository.save(exdeBeforeContractSpecialClause);
	}


	/**
	 * 保存合同签订前审批其它注意条款子表
	 */
	@Override
	public void saveExdeBeforeContractNotices(ExdeBeforeContractNotices exdeBeforeContractNotices,
			List<ExdeBeforeContractNotices> contracts, boolean isInit) {
		for(int i=0;i<contracts.size();i++){
			ExdeBeforeContractNotices a = contracts.get(i);
			int b = a.getnOrderNumber();
			if(isInit){
				if(b>=exdeBeforeContractNotices.getnOrderNumber())
					a.setnOrderNumber(b+1);
			}else{
				ExdeBeforeContractNotices old = exdeBeforeContractNoticesRepository.findOne(exdeBeforeContractNotices.getId());
				if(exdeBeforeContractNotices.getnOrderNumber()<old.getnOrderNumber()){
					if(i<old.getnOrderNumber()-1 && i>=exdeBeforeContractNotices.getnOrderNumber()-1){
						a.setnOrderNumber(b+1);
					}
				}else if(exdeBeforeContractNotices.getnOrderNumber()>old.getnOrderNumber()){
					if(i>old.getnOrderNumber()-1 && i<=exdeBeforeContractNotices.getnOrderNumber()-1){
						a.setnOrderNumber(b-1);
					}
				}
			}
			exdeBeforeContractNoticesRepository.save(a);
		}
		exdeBeforeContractNoticesRepository.save(exdeBeforeContractNotices);
	}


	/**
	 * 保存合同签订前审批合同合同标的物子表的子表
	 */
	@Override
	public void saveExdeBeforeContractDetailParts(ExdeBeforeContractDetailParts exdeBeforeContractDetailParts,
			List<ExdeBeforeContractDetailParts> contracts, boolean isInit) {
		for(int i=0;i<contracts.size();i++){
			ExdeBeforeContractDetailParts a = contracts.get(i);
			int b = a.getnOrderNumber();
			if(isInit){
				if(b>=exdeBeforeContractDetailParts.getnOrderNumber())
					a.setnOrderNumber(b+1);
			}else{
				ExdeBeforeContractDetailParts old = exdeBeforeContractDetailPartsRepository.findOne(exdeBeforeContractDetailParts.getId());
				if(exdeBeforeContractDetailParts.getnOrderNumber()<old.getnOrderNumber()){
					if(i<old.getnOrderNumber()-1 && i>=exdeBeforeContractDetailParts.getnOrderNumber()-1){
						a.setnOrderNumber(b+1);
					}
				}else if(exdeBeforeContractDetailParts.getnOrderNumber()>old.getnOrderNumber()){
					if(i>old.getnOrderNumber()-1 && i<=exdeBeforeContractDetailParts.getnOrderNumber()-1){
						a.setnOrderNumber(b-1);
					}
				}
			}
			exdeBeforeContractDetailPartsRepository.save(a);
		}
		exdeBeforeContractDetailPartsRepository.save(exdeBeforeContractDetailParts);
	}


	/**
	 * 保存合同签订前审批外购件独家指定子表
	 */
	@Override
	public void saveExdeBeforeContractWGDetail(ExdeBeforeContractWGDetail exdeBeforeContractWGDetail,
			List<ExdeBeforeContractWGDetail> contracts, boolean isInit) {
		for(int i=0;i<contracts.size();i++){
			ExdeBeforeContractWGDetail a = contracts.get(i);
			int b = a.getnOrderNumber();
			if(isInit){
				if(b>=exdeBeforeContractWGDetail.getnOrderNumber())
					a.setnOrderNumber(b+1);
			}else{
				ExdeBeforeContractWGDetail old = exdeBeforeContractWGDetailRepository.findOne(exdeBeforeContractWGDetail.getId());
				if(exdeBeforeContractWGDetail.getnOrderNumber()<old.getnOrderNumber()){
					if(i<old.getnOrderNumber()-1 && i>=exdeBeforeContractWGDetail.getnOrderNumber()-1){
						a.setnOrderNumber(b+1);
					}
				}else if(exdeBeforeContractWGDetail.getnOrderNumber()>old.getnOrderNumber()){
					if(i>old.getnOrderNumber()-1 && i<=exdeBeforeContractWGDetail.getnOrderNumber()-1){
						a.setnOrderNumber(b-1);
					}
				}
			}
			exdeBeforeContractWGDetailRepository.save(a);
		}
		exdeBeforeContractWGDetailRepository.save(exdeBeforeContractWGDetail);
	}
	
	
}
